/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import entities.Contact;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.aptech.session.beans.ContactFacade;

/**
 *
 * @author Chien
 */
@ManagedBean
@RequestScoped
public class ContactControl {
    @EJB
    private ContactFacade contactFacade;
    private Contact contact;

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }
    

    /**
     * Creates a new instance of ContactControl
     */
    public ContactControl() {
        contact=new Contact();
    }
    public List<Contact> getAllContact()
    {
        return contactFacade.findAll();
    }
    public void insert()
    {
        try {
            contactFacade.create(contact);
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/ContactManager.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ContactControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void update()
    {
        try {
            contactFacade.edit(contact);
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/ContactManager.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ContactControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void delete(Contact item)
    {
        try {
            contactFacade.remove(item);
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/ContactManager.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ContactControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void showDeteil(Contact item)
    {
        try {
            this.contact=item;
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/UpdateManager.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ContactControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
